package com.youfan.book.controller;

import com.youfan.book.domain.Book;
import com.youfan.book.service.BookService;
import com.youfan.book.service.BookrackService;
import com.youfan.book.service.BooktypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/book")
@Controller
public class BookController {
    @Autowired
    private BookService bookService;
    @Autowired
    private BookrackService bookrackService;
    @Autowired
    private BooktypeService booktypeService;

    @GetMapping("book")
    public String book(Model model) {
        model.addAttribute("list", bookService.list());
        return "/book/book";
    }

    @GetMapping("book_add")
    public String bookAdd(Model model) {
        model.addAttribute("bookrackList", bookrackService.list());
        model.addAttribute("booktypeList", booktypeService.list());
        return "/book/book_add";
    }

    @PostMapping("book_add_do")
    public String bookAddDo(Book book) {
        bookService.save(book);
        return "redirect:/book/book";
    }

    @GetMapping("book_modify")
    public String bookModify(Model model, Long id) {
        model.addAttribute("item", bookService.getById(id));
        model.addAttribute("bookrackList", bookrackService.list());
        model.addAttribute("booktypeList", booktypeService.list());
        return "/book/book_Modify";
    }

    @PostMapping("book_modify_do")
    public String bookModifyDo(Book book){
        bookService.updateById(book);
        return "redirect:/book/book";
    }

    @GetMapping("book_delete")
    public String bookDelete(Long id){
        bookService.removeById(id);
        return "redirect:/book/book";
    }
}
